package org.meal.controller;

import cn.meal.dto.MenuDto;
import cn.meal.entity.Menu;
import cn.meal.entity.Merchant;
import cn.meal.entity.Store;
import cn.meal.vo.MerchantVo;
import cn.meal.vo.ParentMenuVo;
import cn.meal.vo.StoreListVo;
import cn.util.common.Result;
import cn.util.common.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.meal.service.MenuService;
import org.meal.service.MerchantService;
import org.meal.service.StoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Api(tags = "为饭店老板提供的接口")
@RestController
@RequestMapping("/meal/api/merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;
    @Autowired
    private StoreService storeService;

    @Autowired
    private MenuService menuService;


    /**
     * 生成二维码
     * @param storeId
     * @param desk
     * @param response
     * @param request
     */
    @GetMapping("qrCode/{storeId}/{desk}")
    public void getCodeImage(@PathVariable("storeId")Integer storeId,
                             @PathVariable("desk")String desk,
                             HttpServletResponse response,
                             HttpServletRequest request){
        //String headUrl=request.getHeader();
        String url="http://localhost:8280/meal/api/merchant/menu/1/1";

    }

    /**
     * 添加餐馆
     * @param store
     * @return
     */
    @ApiOperation("添加饭店接口")
    @PostMapping("store")
    public Result addStore(@RequestBody Store store,
                           HttpServletRequest request){

       if(store==null){
          return Result.fall();
       }
       Integer merchantId=this.getMerChantId(request);
       store.setMerchantId(merchantId);
       store.setCreateTime(new Date());
       store.setUpdateTime(new Date());
       // 保存饭店信息
        storeService.save(store);
        return Result.ok();
    }


    /**
     * 更新饭店信息
     * @param store
     * @return
     */
    @ApiOperation("修改饭店信息")
    @PutMapping("store")
    public Result updateStore(@RequestBody Store store){
        if(store==null)
        {
            return Result.fall();
        }
        store.setUpdateTime(new Date());
        storeService.updateById(store);
        return Result.ok();
    }

    /**
     * 得到老板名下的饭店列表
     * @param request
     * @return
     */
    @ApiOperation("得到老板的饭店列表")
    @GetMapping("store")
    public Result getStoreList(HttpServletRequest request){
        Integer merchantId=this.getMerChantId(request);
        List<StoreListVo> list=storeService.getStoreByMerchantId(merchantId);
        return Result.ok(list);
    }

    /**
     * 得到饭店的详细信息
     * @param storeId
     * @return
     */
    @ApiOperation("得到饭店的详细信息")
    @GetMapping("store/{storeId}")
    public Result getStoreInfo(@PathVariable("storeId")Integer storeId){

        Store store=storeService.getById(storeId);
        return Result.ok(store);
    }

    /**
     * 添加菜单
     * @param menuDto
     * @return
     */
    @ApiOperation("添加一条菜单记录")
    @PostMapping("menu")
    public Result addMenu(@RequestBody MenuDto menuDto){
        if(menuDto==null){
            return Result.fall();
        }
        Menu menu=new Menu();
        BeanUtils.copyProperties(menuDto,menu);
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());
        //添加
        menuService.saveMenu(menu);
        return Result.ok();
    }

    /**
     * 更新菜单
     * @param menuDto
     * @return
     */
    @ApiOperation("更新一条菜单记录")
    @PutMapping("menu")
    public Result updateMenu(@RequestBody MenuDto menuDto){

        if(menuDto==null){
            return Result.fall();
        }
        Menu menu=new Menu();
        BeanUtils.copyProperties(menuDto,menu);
        menu.setUpdateTime(new Date());
        menuService.updateMenu(menu);
        return Result.ok();
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @ApiOperation("删除菜单记录")
    @DeleteMapping("menu/{menuId}")
    public Result deleteMenu(@PathVariable("menuId")Integer menuId){
        // 在提交之前，前端先进行确定是否进行删除，如果是父级菜单，子菜单也一起删除
        // 这里的删除是进行逻辑删除。

        menuService.deleteMenuByMenuId(menuId);
        return Result.ok();
    }

    /**
     * 得到饭店下的所有父级ID
     * @param storeId
     * @return
     */
    @ApiOperation("得到饭店下面的菜单分类")
    @GetMapping("menu/{storeId}")
    public Result getParentMenu(@PathVariable("storeId")Integer storeId){

        List<ParentMenuVo> parentMenuVos=menuService.getParentMenuByStoreId(storeId);

        return Result.ok(parentMenuVos);
    }

    /**
     * 得到子菜单
     * @param storeId
     * @param parentId
     * @return
     */
    @ApiOperation("得到饭店下指定菜单")
    @GetMapping("menu/{storeId}/{parentId}")
    public Result getMenus(@PathVariable("storeId")Integer storeId,
                           @PathVariable("parentId")Integer parentId){

        Menu menus = menuService.getMenus(storeId,parentId);
        return Result.ok(menus);
    }

    /**
     * 保存用户信息
     * @param merchant
     * @return
     */
    @ApiOperation("用户信息完善修改")
    @PostMapping("/real")
    public Result addMerchantInfo(@RequestBody Merchant merchant){
        //如果用户信息为空，返回错误状态码
        if(merchant==null){
            Result.fall();
        }
        //设置用户信息
        merchant.setUpdateTime(new Date());
        merchant.setUpdateBy(merchant.getId());
        //保存用户信息
        merchantService.updateById(merchant);
        return Result.ok();
    }

    /**
     * 根据商家ID得到认证信息
     * @param
     * @param request
     * @return
     */
    @ApiOperation("得到用户的详细信息")
    @GetMapping("real")
    public Result getMerchantInfo(HttpServletRequest request){
        // 1.获得登录用户token
        //String token=request.getHeader("token");
        // 2.通过token获得用户ID
        Integer merchantId=this.getMerChantId(request);
        // 3.通过ID得到用户的详细信息
        Merchant merchant=merchantService.getById(merchantId);
        // 4.复制可以查看的用户信息
        MerchantVo merchantVo= new MerchantVo();
        BeanUtils.copyProperties(merchant,merchantVo);
        return Result.ok(merchantVo);
    }


    /**
     * 老板登录，需要通过手机号收验证码登录
     * @return
     */
    @ApiOperation("得到验证--代完成")
    @PostMapping("code")
    public Result getUserCode(String phone){

        return Result.ok("1234");
    }

    /**
     * 用户登录
     * @param phone
     * @param code
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("login")
    public Result toLogin(String phone,String code){
        // 1.判断手机号和验证码是否正确
        if(!merchantService.verifyPhoneAndCode(phone,code)){
            return Result.fall(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMessage());
        }
        // 2.验证通过获得token
        String token=merchantService.getTokenByPhone(phone);
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        return Result.ok(map);
    }

    private Integer getMerChantId(HttpServletRequest request) {
        String token = request.getHeader("token");
        return 1;
    }
}
